package com.itheima.web.manager;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.domain.db.Admin;
import com.itheima.service.db.AdminService;
import com.itheima.util.ConstantUtil;
import com.itheima.util.JwtUtil;
import com.itheima.web.exception.BusinessException;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class AdminManeger {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //生成验证码
    public LineCaptcha verification(String uuid) {
//        System.out.println("uuid--" + uuid);
        LineCaptcha lineCaptcha = new LineCaptcha(200, 100);
        String code = lineCaptcha.getCode();
        System.out.println("验证码为---" + code);
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_CODE + uuid, code, 5, TimeUnit.MINUTES);
        return lineCaptcha;
    }

    @DubboReference
    private AdminService adminService;

    //登录校验
    public ResponseEntity login(String username, String password, String verificationCode, String uuid) {
        String redisCode = stringRedisTemplate.opsForValue().get(ConstantUtil.ADMIN_CODE + uuid);
        if (!StrUtil.equals(redisCode, verificationCode)) {
            throw new BusinessException("验证码错误");
        }
        Admin admin = adminService.findByUsernameAndPassword(username);
        if (admin == null) {
            throw new BusinessException("用户名不存在");
        }
        String md5Pwd = SecureUtil.md5(password);
        if (!StrUtil.equals(admin.getPassword(), md5Pwd)) {
            throw new BusinessException("密码错误");
        }
        admin.setPassword(null);
        Map<String, Object> map = BeanUtil.beanToMap(admin);
        String token = JwtUtil.createToken(map);
        String jsonString = JSON.toJSONString(map);
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_TOKEN+token,jsonString,30,TimeUnit.MINUTES);
        Map returnMap = new HashMap();
        returnMap.put("token",token);
        //删除验证码缓存
        stringRedisTemplate.delete(ConstantUtil.ADMIN_CODE+uuid);
        return ResponseEntity.ok(returnMap);
    }

    //前端携带token 获取用户基本信息
    public Admin profile(String token) {
        token= token.replaceAll("Bearer ","");
        String json = stringRedisTemplate.opsForValue().get(ConstantUtil.ADMIN_TOKEN + token);
        if (StrUtil.isEmpty(json)){
            throw new BusinessException("请刷新登录页面.重新登录");
        }
        Admin admin = JSON.parseObject(json, Admin.class);
        //续期操作
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_TOKEN+token,json,30,TimeUnit.MINUTES);
        return admin;
    }

    public ResponseEntity logout(String token) {
        token= token.replaceAll("Bearer ","");
        stringRedisTemplate.delete(ConstantUtil.ADMIN_TOKEN+token);
        return ResponseEntity.ok(null);
    }
}
